package com.ddstation.exchange.controller;

import com.ddstation.common.model.InvsCheckCommonSchParModel;
import com.ddstation.common.service.ViolationMessageService;
import com.ddstation.common.util.CheckUtil;
import com.ddstation.common.util.DateUtil;
import com.ddstation.crm.batchoutput.enmu.ExcelCellDataType;
import com.ddstation.crm.batchoutput.service.ExcelUtils;
import com.ddstation.crm.login.bean.SessionLoginData;
import com.ddstation.crm.outpatient.model.OutpatientModel;
import com.ddstation.crud.entity.DtInvsViolationManageEntity;
import com.ddstation.crud.entity.MaInvsViolationMessageEntity;
import com.ddstation.exam.entity.DtHealthManageInstitutionGroupEntity;
import com.ddstation.exam.entity.MaInvsDataExchangeStatisticsEntity;
import com.ddstation.exam.mapper.MaInvsDataExchangeStatisticsMapper;
import com.ddstation.exchange.model.MaInvsDataExchangeStatisticsModel;
import com.ddstation.exchange.service.DtHealthManageInstitutionGroupService;
import com.ddstation.prescription_violate.model.InstitutionViolateResModel;
import com.ddstation.prescription_violate.model.ViolateDetailParaModel;
import com.ddstation.prescription_violate.model.ViolateDetailResModel;
import com.ddstation.prescription_violate.service.PrescriptionViolateService;
import com.ddstation.sewage.service.SewageService;
import com.ddstation.util.getDateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller @Lazy
public class ExchangeStatisticsController {
	
	// 页面初始化
	@RequestMapping(value = "invs_exchange_statistics.do")
	public String prescriptionViolateEvent(
			Model model) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 获取当天
		Calendar cale = Calendar.getInstance();
		String lastday = format.format(cale.getTime());
		//取开始时间
		String firstday = getDateUtil.getFirstday(1);

		model.addAttribute("dtFrom", firstday); 	// 开始时间
		model.addAttribute("dtTo", lastday); 		// 当天
		return "pc/exchange_statistics/exchange_statistics";
	}
	
	/**
	 * 数据查询接口
	 * 
	 * @return
	 */
	@RequestMapping(value="invs_exchange_statistics.do/load", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> loadPrescriptionViolateListEvent(
			@RequestParam String dtFrom,
			@RequestParam String dtTo,
			@RequestParam(required=false)Integer type,
			@RequestParam(required=false) String tableName,
			HttpSession session){
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY );

		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("errcode", -1);
		ret.put("errMsg", "系统出错，请稍后再试！");
		//查询条件
		MaInvsDataExchangeStatisticsModel _p = new MaInvsDataExchangeStatisticsModel();
		_p.setDtFrom(dtFrom);
		_p.setDtTo(dtTo + " 23:59:59");//默认到当天最后一秒
		_p.setType(type);
		if(tableName != null && !"".equals(tableName)){
			//表名不为空则模糊查询
			_p.setTableName("%"+tableName+"%");
		}
		//查询数据
		List<MaInvsDataExchangeStatisticsEntity> statisticsList = exchangeStatisticsMapper.statisticsByDate(_p);

		ret.put("statisticsList", statisticsList);
        
		ret.put("errcode", 0);
		ret.put("errMsg", "");
		return ret;
	}


	/**
	 * 数据查询接口
	 *
	 * @return
	 */
	@RequestMapping(value="invs_exchange_statistics.do/load_detail", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> loadDetailPrescriptionViolateListEvent(
			@RequestBody MaInvsDataExchangeStatisticsModel _p,
			HttpSession session){
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY );

		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("errcode", -1);
		ret.put("errMsg", "系统出错，请稍后再试！");
		//查询条件
		_p.setDtTo(_p.getDtTo() + " 23:59:59");//默认到当天最后一秒
		//查询数据
		List<MaInvsDataExchangeStatisticsEntity> statisticsList = exchangeStatisticsMapper.statisticsByTable(_p);

		ret.put("statisticsList", statisticsList);

		ret.put("errcode", 0);
		ret.put("errMsg", "");
		return ret;
	}

	@Inject private MaInvsDataExchangeStatisticsMapper exchangeStatisticsMapper;;

}
